﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;

namespace WP.RM.Form
{
    public partial class frmRMWorkQrySetting : BaseQryRpt
    {

        public enum FromSettingType
        {
            /// <summary>
            /// frmRMWorkQtyInputQry
            /// </summary>
            考勤录入查询 =0,  

            /// <summary>
            /// frmRMWorkQtyPruductQry
            /// </summary>
            产量录入查询,

            /// <summary>
            /// frmRMInputQryByWorker
            /// </summary>
            人员产量录入查询
        }
        bool blnPressExit = true;
        FromSettingType mstrFromType = FromSettingType.产量录入查询;
        public frmRMWorkQrySetting(FromSettingType strFromType)
        {
            InitializeComponent();            
            LoadAllControl(new ArrayList(this.Controls));
            this.MinimumSize = this.Size;
            mstrFromType = strFromType;
        }

        public string GetCondition()
        {
            this.ShowDialog();
            if (blnPressExit == true) return "";
            string strCond = " where 1=1 ";

            if (mstrFromType != FromSettingType.人员产量录入查询)
            {
                strCond += " and Machine.MachineSeqNo>='" + txtStMachineSeqNo.Text + "' and Machine.MachineSeqNo<='" + txtEdMachineSeqNo.Text + "'";
                strCond += " and WKGroup.SeqNo >='" + txtStWorkGroupSeqNo.Text + "' and WKGroup.SeqNo<='" + txtEdWorkGroupSeqNo.Text + "'";
                strCond += " and WKProcedure.WorkingProcedureID>='" + txtStWorkingProcedureID.Text + "' and WKProcedure.WorkingProcedureID<='" + txtEdWorkingProcedureID.Text + "'";
            }

            if (cmbWorkShopType.SelectedIndex == 0) //作业车间
            {
                strCond += " and WKShop.WorkShopSeqNo>='" + txtStWorkShop.Text + "' and WKShop.WorkShopSeqNo<='" + txtEdWorkShop.Text + "'";
            }
            else
            {
                strCond +=" and Worker.WorkShopSeqNo>='"+ txtStWorkShop.Text +"' and Worker.WorkShopSeqNo<='"+ txtEdWorkShop.Text +"'";
            }            
            strCond += " and Worker.WorkerID>='"+ txtStWorkerID.Text +"' and Worker.WorkerID<='"+ txtEdWorkerID.Text +"'";                    
            if (BaseFunctions.IsDate(datStWorkDate.Value))
            {
                strCond += " and Hdr.WorkDate>='" + datStWorkDate.Value + "'";
            }
            if (BaseFunctions.IsDate(datEdWorkDate.Value))
            {
                strCond += " and Hdr.WorkDate<='" + datEdWorkDate.Value + "'";
            }            
            return strCond;
        }

        private void initControl()
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strMaxValue = "", strMinValue = "";

            cmbWorkShopType.Items.Clear();
            cmbWorkShopType.Items.Add("作业车间");
            cmbWorkShopType.Items.Add("所属车间");
            cmbWorkShopType.SelectedIndex = 0;

            DataFunctions.GetMaxMinRec(dbcon, "rmMachine", "MachineSeqNo", ref strMaxValue, ref strMinValue);
            txtStMachineSeqNo.Text = strMinValue;
            txtEdMachineSeqNo.Text = strMaxValue;

            DataFunctions.GetMaxMinRec(dbcon, "rmWorkGroup", "SeqNo", ref strMaxValue, ref strMinValue);
            txtStWorkGroupSeqNo.Text = strMinValue;
            txtEdWorkGroupSeqNo.Text = strMaxValue;

            DataFunctions.GetMaxMinRec(dbcon, "rmWorkShop", "WorkShopSeqNo", ref strMaxValue, ref strMinValue);
            txtStWorkShop.Text = strMinValue;
            txtEdWorkShop.Text = strMaxValue;

            DataFunctions.GetMaxMinRec(dbcon, "rmWorkingProcedure", "WorkingProcedureID", ref strMaxValue, ref strMinValue);
            txtStWorkingProcedureID.Text = strMinValue;
            txtEdWorkingProcedureID.Text = strMaxValue;

            DataFunctions.GetMaxMinRec(dbcon, "rmWorker", "WorkerID", ref strMaxValue, ref strMinValue);
            txtStWorkerID.Text = strMinValue;
            txtEdWorkerID.Text = strMaxValue;

            datEdWorkDate.Value =  BaseFunctions.FormatDate ( DataFunctions.GetPrdInfo ( SystemModules.RM , PeriodRange.PeriodCur, PeriodOption.EnPrdDate));
            datStWorkDate.Value = BaseFunctions.FormatDate ( DataFunctions.GetPrdInfo ( SystemModules.RM , PeriodRange.PeriodCur, PeriodOption.StPrdDate));

            if (mstrFromType == FromSettingType.人员产量录入查询) // 设置 其他选项不可见
            {
                pnlOtherInfo.Visible = false;
            }
        }

        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strFindValue = string.Empty;
            string strFieldName = string.Empty;
            string strSql = string.Empty;
            string strCaption = string.Empty;
            Control con = sender as Control;
            if (con == null) return;
            Control ocon = null;
            switch (con.Name)
            {
                case "ubtStWorkGroup":
                case "ubtEdWorkGroup":
                    strFindValue = (con.Name == "ubtStProductName" ? txtStWorkGroupSeqNo.Text : txtEdWorkGroupSeqNo.Text);
                    strFieldName = "SeqNo";
                    strSql = BaseFunctions.GetLookUpSQL("rmWorkGroup");
                    ocon = (con.Name == "ubtStWorkGroup" ? txtStWorkGroupSeqNo : txtEdWorkGroupSeqNo);
                    break;
                case "ubtStMachineSeqNo":
                case "ubtEdMachineSeqNo":
                    strFindValue = (con.Name == "ubtStMachineSeqNo" ? txtStMachineSeqNo.Text : txtEdMachineSeqNo.Text);
                    strFieldName = "MachineSeqNo";
                    strSql = BaseFunctions.GetLookUpSQL("rmMachine");
                    ocon = (con.Name == "ubtStMachineSeqNo" ? txtStMachineSeqNo : txtEdMachineSeqNo);
                    break;
                case "ubtStWorkShop":
                case "ubtEdWorkShop":
                    strFindValue = (con.Name == "ubtWorkShop" ? txtStWorkShop.Text : txtEdWorkShop.Text);
                    strFieldName = "WorkShopSeqNo";
                    strSql = BaseFunctions.GetLookUpSQL("rmWorkShop");
                    ocon = (con.Name == "ubtStWorkShop" ? txtStWorkShop : txtEdWorkShop);
                    break;
                case "ubtStWorkingProcedureID":
                case "ubtEdWorkingProcedureID":
                    strFindValue = (con.Name == "ubtStWorkingProcedureID" ? txtStWorkingProcedureID.Text : txtEdWorkingProcedureID.Text);
                    strFieldName = "WorkingProcedureID";
                    strSql = BaseFunctions.GetLookUpSQL("rmWorkingProcedureID");
                    ocon = (con.Name == "ubtStWorkingProcedureID" ? txtStWorkingProcedureID : txtEdWorkingProcedureID);
                    break;
                case "ubtStWorkerID":
                case "ubtEdWorkerID":
                    strFindValue = (con.Name == "ubtStWorkerID" ? txtStWorkerID.Text : txtEdWorkerID.Text);
                    strFieldName = "WorkerID";
                    strSql = BaseFunctions.GetLookUpSQL("rmWorker");
                    ocon = (con.Name == "ubtStWorkerID" ? txtStWorkerID : txtEdWorkerID);
                    break;
            }
            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;

            frmLookUp frmlookup = new frmLookUp(strSql, strCaption, strFindValue, strFieldName);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                ocon.Text = frmlookup.GetReturnValue(strFieldName);
                ocon.Focus();
            }
            frmlookup.Close();

        }

        private void frmRMWorkQrySetting_Load(object sender, EventArgs e)
        {
            initControl();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            blnPressExit = false;
            this.Close(); 
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            blnPressExit = true;
            this.Close();
        }
    }
}
